﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Web.Security;

namespace Framework.WebmasterTool.Logs
{
    public partial class LogDetails : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadLogDetails();
            }
        }

        public void CheckPermission()
        {
            MembershipUser someUser = Membership.GetUser();
            if (someUser == null)
            {
                Response.Redirect(Globals.ApplicationPath + "LoginAdmin/Default.aspx");
            }
            bool flag = false;
            var user = entity.aspnet_Users.Where(us => us.UserName == someUser.UserName).FirstOrDefault();
            user.aspnet_Roles.Load();
            var rolesForUser = user.aspnet_Roles.ToList();
            foreach (var item in rolesForUser)
            {
                var permission = entity.udv_Permissions.Where(ps => ps.RoleId == item.RoleId).FirstOrDefault();
                if (permission.LogSystem.Value == true)
                {
                    flag = true;
                }
            }
            if (flag == false)
            {
                lblPermissionMessage.Visible = true;
                Panel1.Visible = false;
            }
        }

        public void LoadLogDetails()
        {
            if (Request.QueryString["LogID"] != null)
            {
                int logID;
                int.TryParse(Request.QueryString["LogID"], out logID);
                udv_Log logDetails = entity.udv_Log.Where(lg => lg.LogID == logID).FirstOrDefault();
                if (logDetails != null)
                {
                    lblTitle.Text = logDetails.Title;
                    lblAbstract.Text = logDetails.Abstract;
                    lblDescription.Text = logDetails.Description;
                    lblReleaseDate.Text = logDetails.ReleaseDate;
                    lblExpireDate.Text = logDetails.ExpireDate;
                    lblIsApproved.Text = logDetails.IsApproved;
                    lblStatusID.Text = logDetails.StatusID;
                    lblMetaDescription.Text = logDetails.MetaDescription;
                    lblMetaKeywords.Text = logDetails.MetaKeywords;
                    lblActionedDate.Text = string.Format("{0:dd/MM/yyyy HH:mm:ss}", logDetails.ActionedDate);
                    lblActionBy.Text = logDetails.ActionBy;
                    lblIpAdress.Text = logDetails.IpAdress;
                }
                else
                {
                    Response.Redirect("Default.aspx");
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    }
}